/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.enveloprule.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.envelopinfo.entity.EnvelopInfo;
import com.thinkgem.jeesite.modules.envelopinfo.service.EnvelopInfoService;
import com.thinkgem.jeesite.modules.enveloprule.entity.EnvelopRule;
import com.thinkgem.jeesite.modules.enveloprule.service.EnvelopRuleService;

/**
 * 生成红包规则Controller
 * @author thinkgem
 * @version 2017-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/enveloprule/envelopRule")
public class EnvelopRuleController extends BaseController {

	@Autowired
	private EnvelopInfoService envelopInfoService;
	
	@Autowired
	private EnvelopRuleService envelopRuleService;
	
	@ModelAttribute
	public EnvelopRule get(@RequestParam(required=false) String id) {
		EnvelopRule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = envelopRuleService.get(id);
		}
		if (entity == null){
			entity = new EnvelopRule();
		}
		return entity;
	}
	
	@RequiresPermissions("enveloprule:envelopRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(EnvelopRule envelopRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EnvelopRule> page = envelopRuleService.findPage(new Page<EnvelopRule>(request, response), envelopRule); 
		model.addAttribute("page", page);
		return "modules/enveloprule/envelopRuleList";
	}

	@RequiresPermissions("enveloprule:envelopRule:view")
	@RequestMapping(value = "form")
	public String form(EnvelopRule envelopRule, Model model) {
		model.addAttribute("envelopRule", envelopRule);
		List<EnvelopInfo> envelopInfoList = envelopInfoService.findList(new EnvelopInfo());
		model.addAttribute("envelopInfoList", envelopInfoList);
		return "modules/enveloprule/envelopRuleForm";
	}

	@RequiresPermissions("enveloprule:envelopRule:edit")
	@RequestMapping(value = "save")
	public String save(EnvelopRule envelopRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, envelopRule)){
			return form(envelopRule, model);
		}
		if (envelopRule.getIsNewRecord()) {
			envelopRule.setCreateTime(new java.util.Date());
		}else {
			envelopRule.setModifyTime(new java.util.Date());
		}
		if (envelopRule.getType() != null) {
			EnvelopInfo envelopInfo = envelopInfoService.get(envelopRule.getType());
			if (envelopInfo != null) {
				envelopRule.setName(envelopInfo.getEnvelopName());
				envelopRule.setProductType(envelopInfo.getProductType());
			}
		}
		envelopRuleService.save(envelopRule);
		addMessage(redirectAttributes, "保存红包规则成功");
		return "redirect:"+Global.getAdminPath()+"/enveloprule/envelopRule/?orderBy=createTime desc";
	}
	
	@RequiresPermissions("enveloprule:envelopRule:edit")
	@RequestMapping(value = "delete")
	public String delete(EnvelopRule envelopRule, RedirectAttributes redirectAttributes) {
		envelopRuleService.delete(envelopRule);
		addMessage(redirectAttributes, "删除红包规则成功");
		return "redirect:"+Global.getAdminPath()+"/enveloprule/envelopRule/?orderBy=createTime desc";
	}

}